﻿using EMall.Domain;
using System.Text.Json;

namespace EMall.IdentityService.Menus
{
    public class Menu : AggregateRoot<int>
    {
        public virtual string Name { get; private set; }
        public virtual string Code { get; private set; }
        public virtual MenuType Type { get; private set; }
        public virtual int ParentId { get; private set; }
        public virtual string Permission { get; private set; }
        public virtual string Route { get; private set; }
        public virtual string Path { get; private set; }
        public virtual string Extensions { get; private set; }
        public virtual bool Enabled { get; private set; }
        protected Menu() { }
        public Menu(string name, string code, MenuType type, int parentId, bool enabled)
        {
            Name = name;
            Code = code;
            Type = type;
            ParentId = parentId;
            Enabled = enabled;
            AddDomainEvent(new EntityCreatedDomainEvent<Menu>(this));
        }
        public Menu(string name, string code, MenuType type, int parentId, bool enabled, string permission = "", string route = "", string path = "", string extensions = "")
            : this(name, code, type, parentId, enabled)
        {
            Permission = permission;
            Route = route;
            Path = path;
            Extensions = extensions;
            AddDomainEvent(new EntityCreatedDomainEvent<Menu>(this));
        }
        public void Update(string name, string code, MenuType type, int parentId, bool enabled, string permission, string route, string path, string extensions)
        {
            var originJson = JsonSerializer.Serialize(this);
            Name = name;
            Code = code;
            Type = type;
            ParentId = parentId;
            Enabled = enabled;
            Permission = permission;
            Route = route;
            Path = path;
            Extensions = extensions;
            AddDomainEvent(new EntityUpdatedDomainEvent<Menu>(originJson, this));
        }
        public void UpdateCode(string code)
        {
            var originJson = JsonSerializer.Serialize(this);
            Code = code;
            AddDomainEvent(new EntityUpdatedDomainEvent<Menu>(originJson, this));
        }
    }
}
